<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Movie extends CI_Controller {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->model('movie_model');
        $this->load->model('sound_model');
        $template['current_class'] = "movie";
    }

	public function index()
	{
	    $data['movies'] = $this->movie_model->get_all(20, $this->uri->segment(4));
	    $template['content'] = $this->load->view('admin/movie', $data, TRUE);
		$this->load->view('admin/template', $template);
	}
	
	public function create()
	{
	    $this->form_validation->set_rules('title','description','duration','sound_id','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $movie_editor['title'] = "Create Moive";
	    $movie_editor['form_url'] = site_url('admin/movie/create');
	    $movie_editor['sounds'] = $this->sound_model->get_all();
	    if($this->form_validation->run())
    	{
    	    $title = $this->input->post('title', TRUE);
            $description = $this->input->post('description', TRUE);
            $duration = $this->input->post('duration', TRUE);
            $sound_id = $this->input->post('sound_id', TRUE);
            $movie_id = $this->movie_model->create($title, $description, $duration , $sound_id);
            if ($movie_id) 
            {
                $this->_upload_picture($movie_id);
                redirect('admin/movie');
            }else{
                $movie_editor['errors'] = 'Database error please contact your administrator.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/movie_editor', $movie_editor, TRUE);
		$this->load->view('admin/template', $template);
    	
	}
	function edit($movie_id)
    {
        $this->form_validation->set_rules('title','description','duration','sound_id','required');
        $this->form_validation->set_error_delimiters('<span class="form-msg-error">', '</span>');
	    $movie_editor['title'] = "Edit Moive";
	    $movie_editor['form_url'] = site_url('admin/movie/edit/'.$movie_id);
	    $movie_editor['sounds'] = $this->sound_model->get_all();
	    $movie_editor['movie'] = $this->movie_model->get_by_id($movie_id);
	    if($this->form_validation->run())
    	{
    	    $title = $this->input->post('title', TRUE);
            $description = $this->input->post('description', TRUE);
            $duration = $this->input->post('duration', TRUE);
            $sound_id = $this->input->post('sound_id', TRUE);
            $result = $this->movie_model->update($movie_id,$title, $description, $duration, $sound_id);
            if ($result) 
            {
                $this->_upload_picture($movie_id);
                redirect('admin/movie');
            }else{
                $branch_editor['errors'] = 'Database error please contact your administrator.';
            }
            
    	}else{
    	    
    	}
    	$template['content'] = $this->load->view('admin/movie_editor', $movie_editor, TRUE);
		$this->load->view('admin/template', $template);
    }
    
    function destroy($movie_id)
    {
        $this->movie_model->delete($movie_id);
        redirect('admin/movie');
    }
    
    function _upload_picture($movie_id)
    {
        $config['upload_path'] = './uploads/movie/';
        $config['allowed_types'] = 'jpg';
        $config['file_name'] ="temp" ;
        $config['overwrite'] = TRUE;
        $this->load->library('upload', $config);
        if (! $this->upload->do_upload('picture')) 
        {
            //print_r($this->upload->display_errors());
        }
        else
        {
            $config['overwrite'] = TRUE;
            $config['image_library'] = 'gd2';
            $config['source_image'] = './uploads/movie/temp.jpg';
            $config['maintain_ratio'] = TRUE;
            $config['width'] = 100;
            $config['height'] = 100;
            $config['quality'] = 100;
            $config['new_image'] = "./uploads/movie/".$movie_id.".jpg";
            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
        
        }
    }
    
}